# -*- encoding: utf-8 -*-
"""
Bus Planner
Copyright 2011,2012 Ernesto Angel Celis de la Fuente 
<celisdelafuente@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import cgi
import datetime
import urllib
from urllib import urlencode
import wsgiref.handlers

#import placemat
#import urllib2

## Define which Django version to use
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

## Next three imports read:
## http://code.google.com/p/googleappengine/issues/detail?id=5064
import string
import sys
sys.modules['ssl'] = None

## OAuth required
import httplib2
from authorization.oauth import OAuth
from sql.sqlbuilder import SQL
import ftclient
import gdata.auth
import gdata.client
import gdata.service

## Bus planner modules
from bpauth import Credentials 
from busplanner import HandleUser

class MainPage(webapp.RequestHandler):

    ## Handle the GET request
    def get(self):
        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        template_values = {
            'tableId': Credentials.table_id,
            'infor':'ft_data',
            'headers': 'headers',
        }

        ## TODO Check why are you trying to get the path
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, 
                                                template_values))


    ## Handle POST request.
    def post(self):
        """
        Thanks to whoever posted this in pastebin:
        http://pastebin.com/SmLZNVnj
        Uses the gdata client library to generate a request URL
        for the fusion tables service. A user will be automatically
        redirected from this page to a Google authorisation page.
        gdata.gauth.AeSave provides functionality to store attributes
        regarding authentication as a user completes the process. It's
        recommended that attributes are saved using userid.
        See http://code.google.com/p/gdata-python-client/source/browse/src/gdata/gauth.py
        for more information.
        """
        user = users.get_current_user()
        if user:
            current_uid = users.get_current_user().user_id()

            self.redirect("/")
        else:
            self.redirect(users.create_login_url(
                self.request.uri))


# Everything starts here.
application = webapp.WSGIApplication([('/', MainPage),
                                      ('/authsub', HandleUser)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()


